package com.director.example;

import com.director.annotation.DirectAction;
import com.director.core.DirectContext;
import com.director.core.ProviderType;
import com.director.core.annotation.DirectMethod;
import com.director.core.annotation.DirectParam;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Simone Ricciardi
 * @version 1.0, 05/16/2011
 */
@DirectAction
@Service
public class TestAction {

   @DirectMethod
   public String multiply(int a) {
      return "Result : " + (a * 8);
   }

   @DirectMethod(name = "doEcho")
   public String echo(String message) {
      return message;
   }

   @DirectMethod
   public Date dateTest(Date date) {
      return date;
   }

   @DirectMethod(providerType = ProviderType.POLLING)
   public void poll() {
      String message = "Successfully polled at: " + new SimpleDateFormat("H:m:s").format(new Date());
      DirectContext.get().pushEvent(new MessageEvent(message));
   }

   @DirectMethod
   public List<Map> getTree(String id) {
      List<Map> out = new ArrayList<Map>();
      if("root".equals(id)) {
         for(int i = 1; i <= 5; ++i) {
            Map<String, Object> node = new HashMap<String, Object>();
            node.put("id", "n" + i);
            node.put("text", "Node " + i);
            node.put("leaf", false);
            out.add(node);
         }
      } else if(id.length() == 2) {
         int num = Integer.parseInt(id.substring(1));
         for(int i = 1; i <= 5; ++i) {
            Map<String, Object> node = new HashMap<String, Object>();
            node.put("id", id + i);
            node.put("text", "Node " + num + '.' + i);
            node.put("leaf", true);
            out.add(node);
         }
      }
      return out;
   }

   @DirectMethod
   public List<Company> getGrid(GridParam gridParam) {
      Sort sort = gridParam.getSort().get(0);
      String field = sort.getProperty();
      String direction = sort.getDirection();

      List<Company> data = new ArrayList<Company>();
      if(field.equals("name")) {
         data.add(new Company("ABC Accounting", 50000));
         data.add(new Company("Ezy Video Rental", 106300));
         data.add(new Company("Greens Fruit Grocery", 120000));
         data.add(new Company("Icecream Express", 73000));
         data.add(new Company("Ripped Gym", 88400));
         data.add(new Company("Smith Auto Mechanic", 222980));
      } else {
         data.add(new Company("ABC Accounting", 50000));
         data.add(new Company("Icecream Express", 73000));
         data.add(new Company("Ripped Gym", 88400));
         data.add(new Company("Ezy Video Rental", 106300));
         data.add(new Company("Greens Fruit Grocery", 120000));
         data.add(new Company("Smith Auto Mechanic", 222980));
      }
      if(direction.equals("DESC")) {
         Collections.reverse(data);
      }
      return data;
   }

   @DirectMethod
   public String showDetails(
         @DirectParam(name = "firstName") String first,
         @DirectParam(name = "lastName") String last,
         @DirectParam(name = "age") int age) {
      return String.format("Hi %s %s, you are %s years old.", first, last, age);
   }

}
